package com.freedom.org.controller;

import com.freedom.base.annotation.FreedomPermission;
import com.freedom.base.util.AjaxResult;
import com.freedom.base.util.PageInfo;
import com.freedom.org.domain.Department;
import com.freedom.org.query.DepartmentQuery;
import com.freedom.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author freedom
 */
@RestController
@RequestMapping("/dept")
@Api(value = "部门的api", description = "部门的crud功能")
@FreedomPermission(name = "部门管理")
public class DepartmentController {

    @Autowired
    private IDepartmentService service;


    /**
     * 新曾和修改
     */
    @PostMapping
    @ApiOperation(value = "新增修改")
    @FreedomPermission(name = "部门新增修改")
    public AjaxResult insertOrUpdate ( @RequestBody Department department ) {
        try {
            if (Objects.isNull(department.getId())) {
                // 新增
                service.insert(department);
                return AjaxResult.success("新增成功");
            } else {
                service.update(department);
                return AjaxResult.success("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败");
        }

    }


    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @FreedomPermission(name = "部门删除")
    public AjaxResult delete ( @PathVariable Serializable id ) {
        try {
            service.deleteById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 查询
     */
    @GetMapping
    @ApiOperation(value = "查询部门")
    @FreedomPermission(name = "查询部门")
    public AjaxResult getAll () {
        try {
            List<Department> list = service.getAll();
            return AjaxResult.success(list, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询部门")
    @FreedomPermission(name = "通过id查询部门")
    public AjaxResult getById ( @PathVariable Serializable id ) {
        try {
            service.getById(id);
            return AjaxResult.success(service.getById(id), "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }


    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @PutMapping
    @FreedomPermission(name = "分页查询部门")
    public AjaxResult queryPage ( @RequestBody DepartmentQuery query ) {
        try {
            PageInfo<Department> pageInfo = service.queryPage(query);
            return AjaxResult.success(pageInfo, "分页查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败");
        }
    }


    /**
     * 批量删除
     */
    @PatchMapping
    @FreedomPermission(name = "部门批量删除")
    public AjaxResult batchDelete ( @RequestBody List<Long> ids ) {
        try {
            service.batchDelete(ids);
            return AjaxResult.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败");
        }
    }


    /**
     * 查询上级部门
     */
    @GetMapping("/parent")
    @FreedomPermission(name = "查询上级部门")
    public AjaxResult getParents () {
        try {
            List<Department> parents = service.getParents();
            return AjaxResult.success(parents, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }


}